﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Services.Protocols;
using rsdeploygui.ReportService2010;

namespace rsdeploygui
{
    class DataSet
    {
        private ReportingService2010 rs;

        public DataSet (string url)
        {
            try
            {
                rs = new ReportingService2010();
                rs.Credentials = System.Net.CredentialCache.DefaultCredentials;
                rs.Url = url;
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }
        }

        public byte[] getDefinition(string path)
        {
            return this.rs.GetItemDefinition(path);
        }

        public Boolean Create(string Path, byte[] DSdefinition)
        {
            Boolean success = false;
            // assume the path includes the parent folder and actual name as well
            // strip out the parent folder and name
            String parent = Path.Substring(0,Path.LastIndexOf("/"));
            String name = Path.Substring(Path.LastIndexOf("/")+1);

            try
            {
                Warning[] warnings = null;
                this.rs.CreateCatalogItem("DataSet",name, parent, false, DSdefinition, null, out warnings);
                success = true;
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }


            return success;
        }
        public ItemReferenceData[] getDataSourceItemReferenceData(string DataSetPath)
        {
            ItemReferenceData[] irdDS = null;

            try
            {
                irdDS = this.rs.GetItemReferences(DataSetPath, "DataSource");
            }
            catch (SoapException e)
            {
                Console.WriteLine(e.Detail.InnerText);
            }

            return irdDS;            
        }

        public ReportService2010.DataSource[] getDataSources(string path)
        {
            return this.rs.GetItemDataSources(path);
        }

        public void SetItemDataSources(string Path, ReportService2010.DataSource[] ds)
        {
            this.rs.SetItemDataSources(Path, ds);
        }
    }
}
